﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace GZ.Entity
{
    /// <summary>
    /// 产品口味实体类
    /// </summary>
    public class Etaste
    {
        #region 属性
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string tastename;

        public string TasteName
        {
            get { return tastename; }
            set { tastename = value; }
        }
        private string startkey;

        public string StartKey
        {
            get { return startkey; }
            set { startkey = value; }
        }
        private string endkey;

        public string EndKey
        {
            get { return endkey; }
            set { endkey = value; }
        }

        private int tastevalue;

        public int TasteValue
        {
            get { return this.tastevalue; }
            set { this.tastevalue = value; }
        }
        #endregion

        public DataTable GetTaste()
        {
            string sql = "SELECT * FROM tb_taste";
            return MySqlHelper.ExecuteDataset(GZ.DAL.MySQLHelper.connStr, sql).Tables[0];
        }

        /// <summary>
        /// 添加口味
        /// </summary>
        /// <param name="obj">口味对象</param>
        /// <returns></returns>
        public bool Insert(Etaste obj)
        {
            string sql = @"INSERT INTO tb_taste
                            ( 
                            TasteName,
                            StartKey,
                            EndKey)
                            VALUES
                            ( 
                            @TasteName,
                            @StartKey,
                            @EndKey);";
            MySqlParameter[] parameters = new MySqlParameter[3];
            parameters[0] = new MySqlParameter("@TasteName", obj.TasteName);
            parameters[1] = new MySqlParameter("@StartKey", obj.StartKey);
            parameters[2] = new MySqlParameter("@EndKey", obj.EndKey);
            int val = MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters);
            return val > 0 ? true : false;
        }

        /// <summary>
        /// 根据Id删除口味
        /// </summary>
        /// <param name="Id">口味Id</param>
        /// <returns>True为成功，False为失败</returns>
        public bool Del(int Id)
        {
            string sql = "DELETE FROM tb_taste WHERE Id=@Id";
            MySqlParameter[] parameters = new MySqlParameter[1];
            parameters[0] = new MySqlParameter("@Id", Id);
            int val = MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters);
            return val > 0 ? true : false;
        }

        /// <summary>
        /// 根据商品Id查找口味(前台提取)
        /// </summary> 
        /// <returns>口味列表</returns>
        public DataTable GetTasteByProductId(int ProductId)
        {
            string sql = "SELECT T.Id,T.TasteName,T.StartKey,T.EndKey,PT.Value FROM tb_taste as T  inner JOIN (SELECT * FROM tb_proudct_taste WHERE ProductId=@ProductId) AS PT ON T.Id=PT.TasteId";
            MySqlParameter[] parameters = new MySqlParameter[1];
            parameters[0] = new MySqlParameter("@ProductId", ProductId);
            return MySqlHelper.ExecuteDataset(GZ.DAL.MySQLHelper.connStr, sql,parameters).Tables[0];
        }

        /// <summary>
        /// 根据商品Id查找口味（后台编辑）
        /// </summary> 
        /// <returns>口味列表</returns>
        public DataTable GetTastesByProductId(int ProductId)
        {
            string sql = "SELECT T.Id,T.TasteName,T.StartKey,T.EndKey,PT.Value FROM tb_taste as T  LEFT JOIN (SELECT * FROM tb_proudct_taste WHERE ProductId=@ProductId) AS PT ON T.Id=PT.TasteId";
            MySqlParameter[] parameters = new MySqlParameter[1];
            parameters[0] = new MySqlParameter("@ProductId", ProductId);
            return MySqlHelper.ExecuteDataset(GZ.DAL.MySQLHelper.connStr, sql, parameters).Tables[0];
        }
    }
}
